<template>
	<view class="mainbgc">
		<header-top :messageInfo="{typeFlag:true,message:'盘点单统计明细',liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content flexleft" @click="startPrint">
					<view class="dayinji"></view>
				</view>
			</view>
		</header-top>
		<view class="mainpadding_top ffffff">
			<view class="header flexbetween">
				<view class="headerchild" style="flex: 1;">商品名称</view>
				<view class="headerchild headercenter" style="flex: 1;">规格</view>
				<view class="headerchild headercenter" style="flex: 0.7;">应剩</view>
				<view class="headerchild headercenter" style="flex: 0.7;">实际</view>
				<view class="headerchild headercenter" style="flex: 0.7;color: #ff0000;">相差</view>
				<view class="headerchild headerright" style="flex: 0.7;color: #ff0000;">金额</view>
			</view>
			<view class="content_child " v-for="item in list.goods" :key="item.id">
				<view class="flexbetween">
					<view class="contentchild" style="flex: 1;">{{item.goods_name}}</view>
					<view class="contentchild headercenter" style="flex: 1;">{{item.goods_unit}}</view>
					<view class="contentchild headercenter" style="flex: 0.7;">{{item.stock}}件</view>
					<view class="contentchild headercenter" style="flex: 0.7;">{{item.invertory_stock}}件</view>
					<view class="contentchild headercenter" style="flex: 0.7;color: #ff0000;">{{item.profit}}件</view>
					<view class="contentchild headerright" style="flex: 0.7;color: #ff0000;">￥{{item.money}}</view>
				</view>
			</view>
		</view>
		<view class="mainpadding">
			<view class="flexleft flex_wrap">
				<view class="contentchild margin_top margin_right">共：{{list.statistical.goods_kind}}种</view>
				<view class="contentchild margin_top margin_right">应剩库存{{list.statistical.stock}}件</view>
				<view class="contentchild margin_top margin_right">实际库存{{list.statistical.invertory_stock}}件</view>
				<view class="contentchild margin_top margin_right" style="color: #ff0000;">相差{{list.statistical.profit}}件</view>
				<view class="contentchild margin_top margin_right" style="color: #ff0000;">金额{{list.statistical.money}}元</view>
			</view>
			<!-- <view class="flexright">
				<view class="contentchild">合计：</view>
				<view class="miancolor">{{list.statistical.all_money}}元</view>
			</view> -->
		</view>
		<!-- 打印弹出层 -->
		
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	const tsc = require("@/util/ble/tsc.js");
	const esc = require("@/util/ble/esc.js");
	const encode = require("@/util/ble/encoding.js");
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				arr: [],
				type:"",//1退货单，2采购单，3采购退货单，4入库单，5销售单
				list:{
					goods:[
						{
							goods_code: "SP009",
							goods_name: "诺印酸奶12瓶320ml",
							goods_unit: 1,
							id: 26,
							invertory_stock: 12,
							money: 120,
							profit: 12,
							stock: 0,
							unit_name: "件",
							unit_str: "1件",
						}
					],
					statistical:{
						goods_kind: 1,
						invertory_stock: 12,
						money: 120,
						profit: 12,
						stock: 0,
						zhonglei: 1,
					}
				},
				// 打印机技术参数
				dyjshow: false,
				dyj: 0, //0热敏,1针式
				sendContent: "../../../static/image/news/boy.png",
				looptime: 0,
				currentTime: 1,
				lastData: 0,
				oneTimeData: 0,
				returnResult: "",
				canvasWidth: 180,
				canvasHeight: 180,
				imageSrc: '../../static/img/abc_ic_star_black_16dp.png',
				buffSize: [],
				buffIndex: 0,
				printNum: [],
				printNumIndex: 0,
				printerNum: 1,
				currentPrint: 1,
				isReceiptSend: false,
				isLabelSend: false,
				// 打印机技术结束
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.data));
			this.arr = data.arr
			this.init()
			// 打印机
			let that = this;
			let {
				BLEInformation
			} = that.Bluetooth;
			// 打印机结束
		},
		// 打印机开始
		computed: mapState(['sysinfo', 'Bluetooth']),
		onReady() {
			let list = []
			let numList = []
			let j = 0
			for (let i = 20; i < 200; i += 10) {
				list[j] = i;
				j++
			}
			for (let i = 1; i < 10; i++) {
				numList[i - 1] = i
			}
			this.buffSize = list;
			this.oneTimeData = list[0];
			this.printNum = numList;
			this.printerNum = numList[0];
		},
		onUnload() {
			let that = this;
			let {
				BLEInformation
			} = that.Bluetooth;
		},
		// 打印机结束
		onShow() {
			// 打印机开始
			let that = this;
			let width;
			let height;
			uni.getImageInfo({
				src: that.imageSrc,
				success(res) {
					console.log(res.width)
					console.log(res.height)
					width = res.width
					height = res.height
					that.canvasWidth = res.width;
					that.canvasHeight = res.height;
				}
			})
			const ctx = uni.createCanvasContext("edit_area_canvas", this);
			// if (app.globalData.platform == "android") {
			//   ctx.translate(width, height)
			//   ctx.rotate(180 * Math.PI / 180)
			// }
			ctx.drawImage(this.imageSrc, 0, 0, width, height);
			ctx.draw();
			// 打印机结束
		},
		methods: {
			init() {
				httpRequest.request('appapi/manage/goods/pandian_detail', 'POST', {
					purchase_code:this.arr.join(),
				}, false, false, true).then(res => {
					this.list = res.data
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			// 开始打印
			startPrint() {
				this.dyjshow = true
			},
			dyjconfirm() {
				this.dyjshow = false
				if (this.dyj == 0) {
					this.receiptTest()
				} else {
					this.needlePrint()
				}
			},
			// 针式打印
			needlePrint() {
				// 直接调取打印接口数据就行
				httpRequest.request('appapi/manage/prints/statistical_printer', 'POST', {
					type: this.type || 1,
					staff_member_id: this.yuangong.id
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
				}).catch(err => {
					// httpRequest.toast(err.msg);
				})
			},
			// 打印机开始
			receiptTest() {
				var that = this;
				var canvasWidth = that.canvasWidth
				var canvasHeight = that.canvasHeight
				var command = esc.jpPrinter.createNew()
				command.init()
				// 标题
				command.bold(1); //加粗
				command.setFontSize(16); //字体大小
				command.setSelectJustification(1) //居中0左对齐1居中对齐2右对齐
				command.rowSpace(100);
				command.setText("盘点单统计明细");
				command.setPrint();
				command.rowSpace(60); //行间距
				command.bold(0); //取消加粗
				command.setFontSize(0); //正常字体
				// 表格
				command.setSelectJustification(0) //居中0左对齐1居中对齐2右对齐
				command.rowSpace(80); //间距
				command.bold(5); //加粗
				command.setText("商品名称");
				command.setAbsolutePrintPosition(220);
				command.setText("规格");
				command.setAbsolutePrintPosition(350);
				command.setText("应剩");
				command.setAbsolutePrintPosition(480);
				command.setText("实际");
				command.setPrint()
				command.bold(0); //加粗
				that.list.goods.forEach(item => {
					command.setText(item.goods_name);
					command.setAbsolutePrintPosition(220);
					command.setText(item.goods_unit);
					command.setAbsolutePrintPosition(350);
					command.setText(item.stock);
					command.setAbsolutePrintPosition(480);
					command.setText(item.invertory_stock);
					command.setPrint();
					command.setText("相差："+item.profit);
					command.setAbsolutePrintPosition(220);
					command.setText("金额："+item.money);
					command.setPrint();
				})
				command.bold(5); //加粗
				command.setText("共：");
				command.setText(that.list.statistical.goods_kind + "种，");
				command.setText("应剩库存："+that.list.statistical.stock);
				command.setText("，实际库存："+that.list.statistical.invertory_stock);
				command.setPrint()
				command.setText("相差："+that.list.statistical.profit);
				command.setText("，金额："+that.list.statistical.money);
				command.setPrint()
				command.setSelectJustification(1) //居中0左对齐1居中对齐2右对齐
				command.setText("询博技术支持");
				command.setPrintAndFeedRow(3);
				that.isReceiptSend = true;
				that.prepareSend(command.getData());
			},
			//准备发送，根据每次发送字节数来处理分包数量
			prepareSend(buff) {
				console.log(buff);
				let that = this
				let time = that.oneTimeData
				let looptime = parseInt(buff.length / time);
				let lastData = parseInt(buff.length % time);
				console.log(looptime + "---" + lastData)
				this.looptime = looptime + 1;
				this.lastData = lastData;
				this.currentTime = 1;
				that.Send(buff)
			},
			//分包发送
			Send(buff) {
				let that = this
				let {
					currentTime,
					looptime: loopTime,
					lastData,
					oneTimeData: onTimeData,
					printerNum: printNum,
					currentPrint
				} = that;
				let buf;
				let dataView;
				if (currentTime < loopTime) {
					buf = new ArrayBuffer(onTimeData)
					dataView = new DataView(buf)
					for (var i = 0; i < onTimeData; ++i) {
						dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
					}
				} else {
					buf = new ArrayBuffer(lastData)
					dataView = new DataView(buf)
					for (var i = 0; i < lastData; ++i) {
						dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
					}
				}
				console.log("第" + currentTime + "次发送数据大小为：" + buf.byteLength)
				let {
					BLEInformation
				} = that.Bluetooth;
			
				plus.bluetooth.writeBLECharacteristicValue({
					deviceId: BLEInformation.deviceId,
					serviceId: BLEInformation.writeServiceId,
					characteristicId: BLEInformation.writeCharaterId,
					value: buf,
					success: function(res) {
						console.log(res)
					},
					fail: function(e) {
						console.log(e)
					},
					complete: function() {
						currentTime++
						if (currentTime <= loopTime) {
							that.currentTime = currentTime;
							that.Send(buff)
						} else {
							//  uni.showToast({
							// title: '已打印第' + currentPrint + '张',
							//  })
							if (currentPrint == printNum) {
								that.looptime = 0;
								that.lastData = 0;
								that.currentTime = 1;
								that.isReceiptSend = false;
								that.isLabelSend = false;
								that.currentPrint = 1;
							} else {
								currentPrint++;
								that.currentPrint = currentPrint;
								that.currentTime = 1;
								that.Send(buff)
							}
						}
					}
				})
			},
			
			// 打印机结束
		}
	}
</script>

<style>
	.dayinji {
		width: 40rpx;
		height: 40rpx;
		background-image: url("@/static/image/system/my2.png");
		background-size: 100% 100%;
	}
	.miancolor {
		color: #0BBDCB;
	}
	.totle {
		padding-bottom: 150rpx;
	}

	.margin_topx {
		margin-top: 10rpx;
	}

	.huisetext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.lansetext {
		font-size: 24rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #999999;
	}

	.center {
		padding: 0 40rpx;
		margin: 30rpx auto;
		background-color: #fff;
	}

	.foot {
		padding: 0 40rpx;
		margin: 0 auto;
	}

	.headercenter {
		text-align: center;
	}

	.headerchild {
		font-size: 32rpx;
		font-weight: bold;
	}

	.headerbottom {
		font-size: 28rpx;
		font-weight: bold;
	}

	.content {
		padding-top: 30rpx;
	}

	.contentchild {
		font-size: 26rpx;
	}

	.headerright {
		text-align: right;
	}

	.content_child {
		margin: 30rpx 0 0 0;
	}
</style>
